<template>
    <div class="right-properties-panel">
        <h2>属性配置</h2>

        <!-- 机柜属性 -->
        <CabinetProperties v-if="selectedCabinet && !selectedDevice" :cabinet="selectedCabinet"
            @deselect="$emit('deselect-cabinet')" />

        <!-- 设备属性 -->
        <DeviceProperties v-else-if="selectedDevice" :device="selectedDevice" @deselect="$emit('deselect-device')"
            @delete-device="handleDeleteDevice" />

        <!-- 无选中时的默认内容 -->
        <div v-else class="no-selection">
            <p>请选择机柜或设备以查看和编辑属性</p>
            <div class="selection-tips">
                <h4>操作提示：</h4>
                <ul>
                    <li>🖱️ 点击机柜可选中并编辑机柜属性</li>
                    <li>🖱️ 点击设备可选中并编辑设备属性</li>
                    <li>🔄 拖拽鼠标可旋转视角</li>
                    <li>🔍 滚轮可缩放视图</li>
                </ul>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import CabinetProperties from './CabinetProperties.vue'
import DeviceProperties from './DeviceProperties.vue'
import { useDataCenterStore } from '@/stores/datacenter'
import type { Cabinet, Device } from '@/stores/datacenter'

interface Props {
    selectedCabinet: Cabinet | null
    selectedDevice: Device | null
}

const props = defineProps<Props>()

const emit = defineEmits<{
    'deselect-cabinet': []
    'deselect-device': []
    'delete-cabinet': [cabinetId: string]
    'delete-device': [deviceId: string, cabinetId: string]
}>()

const dataCenterStore = useDataCenterStore()

const handleDeleteDevice = () => {
    if (!props.selectedDevice) {
        console.log('没有选中的设备')
        return
    }

    console.log('尝试删除设备:', props.selectedDevice.id, props.selectedDevice.name)

    // 查找设备所属的机柜
    for (const row of dataCenterStore.rows) {
        for (const cabinet of row.cabinets) {
            const deviceExists = cabinet.devices.find(d => d.id === props.selectedDevice!.id)
            if (deviceExists) {
                console.log('找到设备所属机柜:', cabinet.id, cabinet.name)
                // 发送删除事件，传递设备ID和机柜ID
                emit('delete-device', props.selectedDevice.id, cabinet.id)
                return
            }
        }
    }

    console.log('未找到设备所属的机柜')
}
</script>

<style scoped>
.right-properties-panel {
    width: 320px;
    height: 100vh;
    background: var(--color-background);
    border-left: 1px solid var(--color-border);
    padding: 1rem;
    overflow-y: auto;
    box-sizing: border-box;
}

.right-properties-panel h2 {
    margin: 0 0 1rem 0;
    color: var(--color-primary);
    font-size: 1.25rem;
    border-bottom: 2px solid var(--color-primary);
    padding-bottom: 0.5rem;
}

.no-selection {
    text-align: center;
    color: var(--color-text-light);
    margin-top: 2rem;
}

.no-selection p {
    font-size: 1rem;
    margin-bottom: 1.5rem;
}

.selection-tips {
    background: var(--color-background-soft);
    padding: 1rem;
    border-radius: var(--border-radius);
    text-align: left;
}

.selection-tips h4 {
    margin: 0 0 0.5rem 0;
    color: var(--color-primary);
    font-size: 0.9rem;
}

.selection-tips ul {
    margin: 0;
    padding-left: 1rem;
    font-size: 0.85rem;
    line-height: 1.6;
}

.selection-tips li {
    margin-bottom: 0.25rem;
}
</style>